package handler

import (
	"context"
	"github.com/jinzhu/gorm"
	"gomicrodemo/proto/user"
	"gomicrodemo/user_service/service"
)

type handler struct {
	db *gorm.DB
}

func (h handler) UserLogin(ctx context.Context, request *user.UserLoginRequest, response *user.UserLoginResponse) error {

	message, token := service.UserLogin(request.Username, request.Password)
	//给响应参数赋值
	response.Message = message
	response.Token = token
	return nil
}

func (h handler) UserRegistry(ctx context.Context, request *user.UserRegistryRequest, response *user.UserLoginResponse) error {

	username := request.GetUsername()
	password := request.GetPassword()
	email := request.GetEmail()
	message, token := service.UserRegistry(username, password, email)

	//给响应参数赋值
	response.Message = message
	response.Token = token
	return nil
}

/*
对外界提供一个调用微服务的对象,
为什么返回handler{}就可以：因为 type handler struct{}继承了pb.micro.go里面的(go的继承是实现了对象里面的所有成员)
type UserHandler interface {
	UserRegistry(context.Context, *UserRegistryRequest, *UserLoginResponse) error
	UserLogin(context.Context, *UserLoginRequest, *UserLoginResponse) error
}

*/

func NewUserHandler(db *gorm.DB) user.UserHandler {
	return handler{}
}
